export function getNowFormatDate () {
  var date = new Date()
  var seperator1 = '-'
  var seperator2 = ':'
  var month = date.getMonth() + 1
  var strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
    ' ' + date.getHours() + seperator2 + date.getMinutes() +
    seperator2 + date.getSeconds()
  return currentdate
}

export function getRealStringLength (str) {
  var realLength = 0, len = str.length, charCode = -1
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i)
    if (charCode >= 0 && charCode <= 128)
      realLength += 1
    else
      realLength += 2
  }
  return realLength
}

//分辨率调整
export function getScreen () {
  var width = screen.width
  var height = screen.height
  console.log(width, height)
  if (1536 <= width && 864 <= height) {
    return 18
  } else if (1024 < width <= 1600) {
    return 22
  } else {
    return false
  }
}

/**
 * 公有的提示信息模块
 * @param  {[type]} obj        当前组件指针，一般为this
 * @param  {int} status        状态
 * @param  {[type]} handleType 触发提示信息的类别
 */
export function commonNotify (obj, status, handleType) {
  var success_msg = ''
  var error_msg = ''
  switch (handleType) {
    case 'ADD':
      success_msg = '新增成功'
      error_msg = '新增失败'
      break
    case 'SAVE':
      success_msg = '保存成功'
      error_msg = '保存失败'
      break
    case 'UPDATE':
      success_msg = '修改成功'
      error_msg = '修改失败'
      break
    case 'DEL':
      success_msg = '删除成功'
      error_msg = '删除失败'
      break
    case 'GET':
      success_msg = '获取成功'
      error_msg = '获取失败'
      break
  }
  switch (status) {
    case 0:
      obj.$message({
        message: success_msg,
        type: 'success'
      })
      break
    case 1:
      obj.$message({
        message: error_msg,
        type: 'error'
      })
    default:
      obj.$message({
        message: error_msg,
        type: 'error'
      })
      break
  }

}

/**
 * 拼接请求服务器映射表的键
 * @param  {[type]} router_type 路由的type参数
 * @param  {[type]} option_type 完成的操作
 * @return {[type]}             返回映射表的键
 */
export function getPosServertMapKey (router_type, option_type) {
  return router_type + '_' + option_type
}

function SearchNewsTypeMap (key) {
  var map = new Map()
  map.set('technology', '69')
  map.set('talentpolicy', '81')
  map.set('talentservice', '84')
  map.set('projectreportingguide', '83')
  map.set('talentstyle', '70')
  map.set('civilmilitary', '125')
  map.set('historyculture', '181')//历史文化
  map.set('scenicspots', '182')//名胜风景
  map.set('delicioussnack', '183')//风味小吃
  return map.get(key)
}

/**
 * 根据类型返回新闻的频道
 * @param  {[type]} type [description]
 * @return {[type]}      [description]
 */
export function newsTypeMap (type) {

  return SearchNewsTypeMap(type)
}

export function formatterTime (type, time) {
  if (time === '') {
    return
  }
  var formatter = ''
  if (type === 'date') {
    formatter = 'yyyy-MM-dd'
  } else if (type === 'time') {
    return getTimeFormatDate(time)
  } else {
    formatter = type
  }
  var date = new Date(time)
  return formatter.replace(/yyyy/g, date.getFullYear()).replace(/MM/g, date.getMonth() + 1).replace(/dd/g, date.getDate()).replace(/hh/g, date.getHours()).replace(/mm/g, date.getMinutes()).replace(/ss/g, date.getSeconds())
}

function getTimeFormatDate (time) {
  var date = new Date(time)
  var seperator1 = '-'
  var seperator2 = ':'
  var month = date.getMonth() + 1
  var strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }

  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
    ' ' + date.getHours() + seperator2 + date.getMinutes() +
    seperator2 + date.getSeconds()
  console.log(currentdate)
  return currentdate
}

export function getUserRoler () {
  return JSON.parse(localStorage.getItem('user_role'))
}

export function getUserType () {
  var user_roler = JSON.parse(localStorage.getItem('user_role'))
  var name = 'error'
  var s = 0
  for (s in user_roler) {
    if (user_roler[s].role_name === '个人用户' || user_roler[s].role_name === '企业用户') {
      name = user_roler[s].role_name
      break
    }
  }
  localStorage.setItem('user_role_type', name)
}

export function constructMenuTreeData (_data)//构建树形结构的数组
{
  var _res = new Array()
  var FristLevelArray = _data.filter(function (value) {
    return value.parent_menuid == 0
  })
  for (var i = 0; i < FristLevelArray.length; i++) {
    _res[i] = new Object()
    _res[i].id = FristLevelArray[i].id
    _res[i].parent_menuid = FristLevelArray[i].parent_menuid
    _res[i].menu_name = FristLevelArray[i].menu_name
    _res[i].menu_url = FristLevelArray[i].menu_url
    _res[i].menu_index = FristLevelArray[i].menu_index
    _res[i].subsys_id = FristLevelArray[i].subsys_id
    _res[i].subsys_name = FristLevelArray[i].subsys_name
    var SecondLevelArray = _data.filter(function (value) {
      return value.parent_menuid == FristLevelArray[i].id
    })
    if (SecondLevelArray.length > 0) {
      _res[i].children = new Array()
      var _second = _res[i].children
      for (var j = 0; j < SecondLevelArray.length; j++) {
        _second[j] = new Object()
        _second[j].id = SecondLevelArray[j].id
        _second[j].parent_menuid = SecondLevelArray[j].parent_menuid
        _second[j].menu_name = SecondLevelArray[j].menu_name
        _second[j].menu_url = SecondLevelArray[j].menu_url
        _second[j].menu_index = SecondLevelArray[j].menu_index
        _second[j].subsys_id = SecondLevelArray[j].subsys_id
        _second[j].subsys_name = SecondLevelArray[j].subsys_name
        var ThirdLeverArray = _data.filter(function (value) {
          return value.parent_menuid == SecondLevelArray[j].id
        })
        if (ThirdLeverArray.length > 0) {
          _second[j].children = new Array()
          var _third = _second[j].children
          for (var m = 0; m < ThirdLeverArray.length; m++) {
            _third[m] = new Object()
            _third[m].id = ThirdLeverArray[m].id
            _third[m].parent_menuid = ThirdLeverArray[m].parent_menuid
            _third[m].menu_name = ThirdLeverArray[m].menu_name
            _third[m].menu_url = ThirdLeverArray[m].menu_url
            _third[m].menu_index = ThirdLeverArray[m].menu_index
            _third[m].subsys_id = ThirdLeverArray[m].subsys_id
            _third[m].subsys_name = ThirdLeverArray[m].subsys_name
          }
        }

      }
    }
  }
  return _res
}

/**将薪资变为用k表示**/
export function changeSalaryStyle (e) {
  if (e.expected_max_salary === undefined) {
    e.expected_max_salary = e.expected_min_salary + 1000
  } else if (e.expected_min_salary === undefined) {
    e.expected_min_salary = e.expected_max_salary - 1000
  }
  e.salary = formatterSalary(e.expected_min_salary) + '-' + formatterSalary(e.expected_max_salary)
  return e
}

function formatterSalary (str) {
  if (str < 1000) {
    str = '1k'
  } else {
    str = String(str / 1000)
    var arr = str.split('.')
    str = arr[0] + 'K'
  }
  return str
}
